package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
        //新增分类
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增分类成功");
    }
    //分类信息分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        //分页
        Page<Category> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //根据正序排序
        lqw.orderByAsc(Category::getSort);
        //执行查询
        categoryService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }
        //删除分类
    @DeleteMapping
    public R<String> deleteById(@RequestParam("id") Long categoryId){
        categoryService.delete(categoryId);
        return R.success("删除成功");
    }
    //修改分类
    @PutMapping
    public R<String> updata(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }

    //新增菜品
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //根据type查询
        lqw.eq(category.getType()!=null,Category::getType,category.getType());
        //排序
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(lqw);

        return R.success(list);
    }
}
